#include <iostream>
#include <queue>
using namespace std;
void YangHuiTriangle(int n);
int main()
{
cout<<"请输入杨辉三角的层数:";
int x;
cin>>x;
YangHuiTriangle(x);
return 0;
}
void YangHuiTriangle(int n)
{
queue<int> q;
q.push(0);
q.push(1);
int x,y;
for(int i=0;i<n;i++)
{
q.push(0);
for(int j=n;j>i;j--)
cout<<" ";
do
{
x = q.front();
q.pop();
y = q.front();
y!=0? cout<<y<<" " : cout<<" " ;
q.push((x+y));
}while(y!=0);
cout<<endl;
}
}
打印杨辉三角(STL版队列)
最新推荐文章于 2023-11-15 20:35:22 发布